﻿# -*- coding: utf-8 -*-

import nltk

# The following method is used to bind keyword and parameters with Jess string.
def jessSentence( keyword, param):
    jess = ""
    # Split texts[j] on spaces.
    newParam = param.split(" ")
    
    start = 0
    
    # Did not start with a space.
    for i in range(len(newParam)):
        if (len(newParam[i]) == 0):
            start = start + 1
        else :
            break
        
    if (keyword == "Alive" and newParam[start] == "TRUE"):
        jess = 'alive ' + newParam[start + 1]
        
    if (keyword == "Alive" and newParam[start] == "FALSE"):
        jess = 'dead is ' + newParam[start + 1] 
        
    if (keyword == "Job" and newParam[start] == "TRUE"):
        jess = 'character ' + newParam[start + 1] + ' is a ' +  newParam[start + 2]
        
    if (keyword == "Job" and newParam[start] == "FALSE"):
        jess = 'character ' + newParam[start + 1] + ' is not a ' +  newParam[start + 2]
        
    if (keyword == "Link" and newParam[start] == "TRUE"):
        jess = 'character ' + newParam[start + 1] + ' works for ' +  newParam[start + 2]
        
    if (keyword == "Link" and newParam[start] == "FALSE"):
        jess = 'character ' + newParam[start + 1] + ' did not work for ' +  newParam[start + 2]
    
    if (keyword == "Open" and newParam[start] == "TRUE"):
        jess = newParam[start + 1] + ' is open'   
        
    if (keyword == "Open" and newParam[start] == "FALSE"):
        jess = newParam[start + 1] + ' is not open'  
        
    if (keyword == "Stole" and newParam[start] == "TRUE"):
        jess = newParam[start + 1] + ' taken-from ' +  newParam[start + 2]
        
    if (keyword == "Stole" and newParam[start] == "FALSE"):
        jess = newParam[start + 1] + ' not taken-from ' +  newParam[start + 2]
        
    if (keyword == "UpdateBy" and newParam[start] == "TRUE"):
        jess = newParam[start + 1] + ' updated-by ' +  newParam[start + 2]
        
    if (keyword == "UpdateBy" and newParam[start] == "FALSE"):
        jess = newParam[start + 1] + ' not updated-by ' +  newParam[start + 2]
        
    if (keyword == "UpdateTime" and newParam[start] == "TRUE"):
        jess = newParam[start + 1] + ' updated at-time ' +  newParam[start + 2]
        
    if (keyword == "UpdateTime" and newParam[start] == "FALSE"):
        jess = newParam[start + 1] + ' not updated at-time ' +  newParam[start + 2]
        
    if (keyword == "LocTime" and newParam[start] == "TRUE"):
        jess = 'at-loc ' + newParam[start + 1] + ' ' +  newParam[start + 2] + ' at-time ' +  newParam[start + 3] 
        
    if (keyword == "LocTime" and newParam[start] == "FALSE"):
        jess = 'not at-loc ' + newParam[start + 1] + ' ' +  newParam[start + 2] + ' at-time ' +  newParam[start + 3]
        
    if (keyword == "Loc" and newParam[start] == "TRUE"):
        jess = 'at-loc ' + newParam[start + 1] + ' ' +  newParam[start + 2]
        
    if (keyword == "Loc" and newParam[start] == "FALSE"):
        jess = 'not at-loc ' + newParam[start + 1] + ' ' +  newParam[start + 2]
        
    if (keyword == "Left" and newParam[start] == "TRUE" and newParam[start + 2] == "boss"):
        jess = newParam[start + 2] + ' left ' + newParam[start + 1] + ' at-time ' +  newParam[start + 3]
        
    if (keyword == "Left" and newParam[start] == "FALSE" and newParam[start + 2] == "boss"):
        jess = newParam[start + 2] + ' did not leave ' + newParam[start + 1] + ' at-time ' +  newParam[start + 3]
        
    if (keyword == "Left" and newParam[start] == "TRUE" and newParam[start + 2] != "boss"):
        jess = 'employe ' + newParam[start + 2] + ' left ' + newParam[start + 1] + ' at-time ' +  newParam[start + 3]
        
    if (keyword == "Left" and newParam[start] == "FALSE" and newParam[start + 2] != "boss"):
        jess = 'employe ' + newParam[start + 2] + ' did not leave ' + newParam[start + 1] + ' at-time ' +  newParam[start + 3]

    if (keyword == "Belongs" and newParam[start] == "TRUE"):
        jess = newParam[start + 1] + ' belongs to ' +  newParam[start + 2]
        
    if (keyword == "Belongs" and newParam[start] == "FALSE"):
        jess = newParam[start + 1] + ' not belongs to ' +  newParam[start + 2]
        
    if (keyword == "Lost" and newParam[start] == "TRUE"):
        jess = 'character ' + newParam[start + 2] + ' lost ' +  newParam[start + 1]
        
    if (keyword == "Lost" and newParam[start] == "FALSE"):
        jess = 'character ' + newParam[start + 2] + ' did not lost ' +  newParam[start + 1]
        
    if (keyword == "HQCamera" and newParam[start] == "TRUE"):
        jess = newParam[start + 1] + '-unknown seen-on camera at-time ' +  newParam[start + 2]
        
    if (keyword == "HQCamera" and newParam[start] == "FALSE"):
        jess = newParam[start + 1] + '-unknown not seen-on camera at-time ' +  newParam[start + 2]
        
    if (keyword == "SomethingsLocation" and newParam[start] == "TRUE"):
        jess = newParam[start + 1] + ' at-loc ' +  newParam[start + 2]
        
    if (keyword == "SomethingsLocation" and newParam[start] == "FALSE"):
        jess = newParam[start + 1] + ' not at-loc ' +  newParam[start + 2]
        
    if (keyword == "FoundTime" and newParam[start] == "TRUE"):
        jess = 'dead ' + newParam[start + 3] + ' ' + newParam[start + 2] + '-found at-time ' +  newParam[start + 1]
        
    if (keyword == "FoundTime" and newParam[start] == "FALSE"):
        jess = 'dead ' + newParam[start + 3] + ' ' + newParam[start + 2] + '-not-found at-time ' +  newParam[start + 1]
        
    if (keyword == "FoundLocation" and newParam[start] == "TRUE"):
        jess = 'dead ' + newParam[start + 2] + '-found at-loc ' + newParam[start + 3]
        
    if (keyword == "FoundLocation" and newParam[start] == "FALSE"):
        jess = 'not dead ' + newParam[start + 2] + '-found at-loc ' + newParam[start + 3]
        
    if (keyword == "Found" and newParam[start] == "TRUE"):
        jess = newParam[start + 2] + '-found ' + newParam[start + 1]  
        
    if (keyword == "Found" and newParam[start] == "FALSE"):
        jess = newParam[start + 2] + '-not-found ' + newParam[start + 1]
        
    if (keyword == "Found" and newParam[start] == "TRUE" and len(newParam) > 5):
        jess = newParam[start + 1] + ' ' +  newParam[start + 3] + '-found-' + newParam[start + 2]
        
    if (keyword == "Found" and newParam[start] == "FALSE" and len(newParam) > 5):
        jess = newParam[start + 1] + ' ' +  newParam[start + 3] + '-not-found-' + newParam[start + 2]
        
    if (keyword == "Found" and newParam[start] == "TRUE" and newParam[start + 1] == "body"):
        jess = 'dead ' + newParam[start + 1] + '-found at-loc ' + newParam[start + 2]
        
    if (keyword == "Found" and newParam[start] == "FALSE" and newParam[start + 1] == "body"):
        jess = 'not dead ' + newParam[start + 1] + '-found at-loc ' + newParam[start + 2]
        
    if (keyword == "Crash" and newParam[start] == "TRUE"):
        jess = newParam[start + 1] + ' crash at-time ' +  newParam[start + 2]
        
    if (keyword == "Crash" and newParam[start] == "FALSE"):
        jess = newParam[start + 1] + ' did not crash at-time ' +  newParam[start + 2]
        
    if (keyword == "LeftEmployee" and newParam[start] == "TRUE"):
        jess = 'employe ' + newParam[start + 2] + ' left at-time ' + newParam[start + 3]
        
    if (keyword == "LeftEmployee" and newParam[start] == "FALSE"):
        jess = 'employe ' + newParam[start + 2] + ' did not leave at-time ' + newParam[start + 3]
        
    if (keyword == "Secure" and newParam[start] == "FALSE"):
        jess = newParam[start + 1] + ' not secured'
        
    if (keyword == "Secure" and newParam[start] == "TRUE"):
        jess = newParam[start + 1] + ' secured'
        
    if (keyword == "Secure" and newParam[start] == "FALSE"):
        jess = newParam[start + 1] + ' not secured'
        
    if (keyword == "Shot" and newParam[start] == "TRUE"):
        jess = 'precise-' + newParam[start + 1] + ' shot ' + newParam[start + 2]
        
    if (keyword == "Shot" and newParam[start] == "FALSE"):
        jess = 'not precise-' + newParam[start + 1] + ' shot ' + newParam[start + 2]
        
    if (keyword == "Work" and newParam[start] == "TRUE"):
        jess = newParam[start + 1] + ' works ' + newParam[start + 2]
        
    if (keyword == "Work" and newParam[start] == "FALSE"):
        jess = newParam[start + 1] + ' did not work ' + newParam[start + 2]
        
    if (keyword == "Loc" and newParam[start] == "TRUE" and newParam[start + 2] == "work"):
        jess = 'at-' + newParam[start + 2] + ' ' + newParam[start + 1]
        
    if (keyword == "Loc" and newParam[start] == "FALSE" and newParam[start + 2] == "work"):
        jess = 'not-at-' + newParam[start + 2] + ' ' + newParam[start + 1]
    
    if (keyword == "Left" and newParam[start] == "TRUE"):
        jess = newParam[start + 2] + ' left ' + newParam[start + 1] + ' at-time ' + newParam[start + 3]
    
    if (keyword == "Heard" and newParam[start] == "TRUE"):
        jess = newParam[start + 1] + ' heard-' + newParam[start + 2] + ' at-loc ' + newParam[start + 3] + ' at ' + newParam[start + 4]
        
    if (keyword == "RelationWithBoss" and newParam[start] == "TRUE" and newParam[start + 2] == "yacht"):
        jess = 'character ' + newParam[start + 1] + ' was with boss on his ' + newParam[start + 2] + ' ' + newParam[start + 3].replace("+", " ")
        
    if (keyword == "RelationWithBoss" and newParam[start] == "FALSE" and newParam[start + 2] == "yacht"):
        jess = 'character ' + newParam[start + 1] + ' was not with boss on his ' + newParam[start + 2] + ' ' + newParam[start + 3].replace("+", " ")
         
    if (keyword == "RelationWithBoss" and newParam[start] == "TRUE" and newParam[start + 2] == "stole+car"):
        jess = 'character ' + newParam[start + 1] + ' stole car of boss ' + newParam[start + 3].replace("+", " ")
        
    if (keyword == "RelationWithBoss" and newParam[start] == "FALSE" and newParam[start + 2] == "stole+car"):
        jess = 'character ' + newParam[start + 1] + ' did not steal car of boss ' + newParam[start + 3].replace("+", " ")
        
    if (keyword == "RelationWithBoss" and newParam[start] == "TRUE" and newParam[start + 2] == "poker"):
        jess = 'character ' + newParam[start + 1] + ' played poker with boss ' + newParam[start + 3].replace("+", " ")
        
    if (keyword == "RelationWithBoss" and newParam[start] == "FALSE" and newParam[start + 2] == "poker"):
        jess = 'character ' + newParam[start + 1] + ' did not play poker with boss ' + newParam[start + 3].replace("+", " ")
        
    if (keyword == "RelationWithBoss" and newParam[start] == "TRUE" and newParam[start + 2] == "argue"):
        jess = 'character ' + newParam[start + 1] + ' argued with boss ' + newParam[start + 3].replace("+", " ")
        
    if (keyword == "RelationWithBoss" and newParam[start] == "FALSE" and newParam[start + 2] == "argue"):
        jess = 'character ' + newParam[start + 1] + ' argued with boss ' + newParam[start + 3].replace("+", " ")         
        
    jess = jess.replace('backdoor', 'back-door')
    jess = jess.replace('idcard', 'id-card')
    jess = jess.replace('williamhouse', 'william-house')
    return jess;

# Convert a string generate by the language fcfg to jess
def convertToJess( sentence ):
    # Remove filter
   newSentence = sentence.replace("(", " ").replace(")", " ").replace(",", " ").split("&")
   
   keyword = ""
   jess = ""
   for i in range(len(newSentence)):
       for j in range(len(newSentence[i])):
           if (newSentence[i][j] is not ' '):
                keyword += newSentence[i][j]
            
            # Valid if we find a keyword.
           if (len(keyword) != 0 and newSentence[i][j] is ' ') :
               jess += "(" + jessSentence( keyword = keyword, param = newSentence[i].replace(keyword, "")) + ")\n"
               keyword = ""
               break
                    
                
   return jess;
 
f1 = open("story1.txt", "r")
textS1 = f1.read()
f1.close()

f2 = open("story2.txt", "r")
textS2 = f2.read()
f2.close()

f3 = open("story3.txt", "r")
textS3 = f3.read()
f3.close()

texts = [textS1, textS2, textS3]
#errors
#
#

#missing
#

# Replace . by , if necessary. We find pronoun before a .
for j in range(0, 3):
  for i in range(len(texts[j])): 
    if ((i > 0 and ((texts[j][i-1] is 'H' and texts[j][i] is 'e') or (texts[j][i-1] is 'I' and texts[j][i] is 't'))) 
  			or (i > 1 and ((texts[j][i-2] is 'S' and texts[j][i-1] is 'h' and texts[j][i]) is 'e'))):
      if (texts[j][i-2] is '.'):
          texts[j][i-2] = ' ,'
      elif (texts[j][i-3] is '.') :
          texts[j][i-3] = ' ,'
      elif (texts[j][i-4] is '.') :
          texts[j][i-4] = ' ,'
  
  newText = ''
  for i in range(len(texts[j])):
    newText += texts[j][i];
  
  # Split texts[j] on .
  sents = newText.split(".");
  
  # Initialize writer.
  writeFile = open("Jess" + str(j + 1) + ".txt", "w")
  
  # Write the sentence convert in Jess.
  for allresults in nltk.batch_interpret(sents, 'file:Grammar.fcfg'):
    for (synrep, semrep) in allresults: 
      writeFile.write(convertToJess(str(semrep)))
      #synrep.draw()
  
  writeFile.close();
